﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class CAddCarController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /CAddCar/

        public ActionResult Index()
        {
            var caddcars = db.CAddCars.Include(c => c.CModelCar).Include(c => c.CRoueSize).Include(c => c.CStatusRepair).Include(c => c.CStageCar).Where(x => x.RecordStatus == "U");
            return View(caddcars.ToList());
        }

        //
        // GET: /CAddCar/Details/5

        public ActionResult Details(int id = 0)
        {
            CAddCar caddcar = db.CAddCars.Find(id);
            if (caddcar == null)
            {
                return HttpNotFound();
            }
            return View(caddcar);
        }

        //
        // GET: /CAddCar/Create

        public ActionResult Create()
        {
            ViewBag.CarModel = new SelectList(db.CModelCars, "ModelCarId", "ModelCarName");
            ViewBag.RoueSize = new SelectList(db.CRoueSizes, "RoueSizeId", "RoueSize");
            ViewBag.AddCarStatus = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName");
            ViewBag.CarType = new SelectList(db.CStageCars, "StageCarId", "StageCarName");
            ViewBag.AddCarUnit = new SelectList(db.CFBranchs, "BrId", "BrName");
            return View();
        }

        //
        // POST: /CAddCar/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CAddCar caddcar)
        {
            if (ModelState.IsValid)
            {
                caddcar.RecordStatus = "U";
                caddcar.CreateBy = Session["LogedId"].ToString();
                caddcar.CreateWhen = DateTime.Now;
                db.CAddCars.Add(caddcar);
                caddcar.RecordStatus = "U";
                caddcar.CreateBy = Session["LogedId"].ToString();
                caddcar.CreateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CarModel = new SelectList(db.CModelCars, "ModelCarId", "ModelCarName", caddcar.CarModel);
            ViewBag.RoueSize = new SelectList(db.CRoueSizes, "RoueSizeId", "RoueSize", caddcar.RoueSize);
            ViewBag.AddCarStatus = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName", caddcar.AddCarStatus);
            ViewBag.CarType = new SelectList(db.CStageCars, "StageCarId", "StageCarName", caddcar.CarType);
            ViewBag.AddCarUnit = new SelectList(db.CFBranchs, "BrId", "BrName", caddcar.AddCarUnit);
            return View(caddcar);
        }

        //
        // GET: /CAddCar/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CAddCar caddcar = db.CAddCars.Find(id);
            if (caddcar == null)
            {
                return HttpNotFound();
            }
            ViewBag.CarModel = new SelectList(db.CModelCars, "ModelCarId", "ModelCarName", caddcar.CarModel);
            ViewBag.RoueSize = new SelectList(db.CRoueSizes, "RoueSizeId", "RoueSize", caddcar.RoueSize);
            ViewBag.AddCarStatus = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName", caddcar.AddCarStatus);
            ViewBag.CarType = new SelectList(db.CStageCars, "StageCarId", "StageCarName", caddcar.CarType);
            ViewBag.AddCarUnit = new SelectList(db.CFBranchs, "BrId", "BrName", caddcar.AddCarUnit);
            return View(caddcar);
        }

        //
        // POST: /CAddCar/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CAddCar caddcar)
        {
            if (ModelState.IsValid)
            {
                db.Entry(caddcar).State = EntityState.Modified;
                caddcar.UpdateBy = Session["LogedId"].ToString();
                caddcar.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CarModel = new SelectList(db.CModelCars, "ModelCarId", "ModelCarName", caddcar.CarModel);
            ViewBag.RoueSize = new SelectList(db.CRoueSizes, "RoueSizeId", "RoueSize", caddcar.RoueSize);
            ViewBag.AddCarStatus = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName", caddcar.AddCarStatus);
            ViewBag.CarType = new SelectList(db.CStageCars, "StageCarId", "StageCarName", caddcar.CarType);
            ViewBag.AddCarUnit = new SelectList(db.CFBranchs, "BrId", "BrName", caddcar.AddCarUnit);
            return View(caddcar);
        }

        //
        // GET: /CAddCar/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CAddCar caddcar = db.CAddCars.Find(id);
            if (caddcar == null)
            {
                return HttpNotFound();
            }
            return View(caddcar);
        }

        //
        // POST: /CAddCar/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CAddCar caddcar = db.CAddCars.Find(id);
            db.Entry(caddcar).State = EntityState.Modified;
            caddcar.RecordStatus = "D";
            caddcar.UpdateBy = Session["LogedId"].ToString();
            caddcar.UpdateWhen = DateTime.Now;
            //db.CAddCars.Remove(caddcar);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}